#!/bin/bash
# 带进度显示的子模块初始化脚本

GIT_CMD="/c/Program Files/Git/bin/git.exe"

echo "=== 子模块初始化（带进度显示）==="
echo

# 检查环境
if ! "$GIT_CMD" rev-parse --git-dir > /dev/null 2>&1; then
    echo "❌ 当前目录不是 Git 仓库"
    exit 1
fi

if [ ! -f ".gitmodules" ]; then
    echo "ℹ️  没有找到 .gitmodules 文件"
    exit 0
fi

echo "当前镜像配置:"
"$GIT_CMD" config --local --list | grep -i insteadof
echo

echo "子模块状态:"
"$GIT_CMD" submodule status
echo

read -p "是否继续初始化子模块? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "操作取消"
    exit 0
fi

echo "开始初始化子模块..."
echo "这可能需要一些时间，请耐心等待..."
echo

# 逐个初始化子模块，显示进度
submodules=$("$GIT_CMD" config --file .gitmodules --get-regexp path | awk '{print $2}')
total=$(echo "$submodules" | wc -l)
current=0

for submodule in $submodules; do
    current=$((current + 1))
    echo "[$current/$total] 初始化: $submodule"
    
    # 初始化单个子模块
    if "$GIT_CMD" submodule update --init "$submodule"; then
        echo "   ✅ 成功"
    else
        echo "   ❌ 失败"
    fi
    echo
done

echo "=== 子模块初始化完成 ==="
"$GIT_CMD" submodule status
